//
//  MainTabBar.swift
//  CXWeibo
//
//  Created by mac on 15/6/6.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

    // 子控制器按钮的位置
    
    private let buttonCount = 5
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        var index = 0
        let w = self.bounds.size.width / CGFloat(buttonCount)
        let h = self.bounds.size.height
        let frame = CGRectMake(0, 0, w, h)
        
        for view in self.subviews as! [UIView]{
        
            // 判断子试图类型
           if(view is UIControl && !(view is UIButton)){
            
//                view.frame = CGRectMake(CGFloat(index) * w, 0, w, h)
//                
//                index++
            view.frame = CGRectOffset(frame, w * CGFloat(index), 0)
            
            index += (index == 1) ? 2:1
            
            }
        }
        // 撰写按钮
        composeButton.frame = frame
        composeButton.center = CGPointMake(self.center.x, h * 0.5)
        
    }
    
    
    
    
    /// 撰写按钮
    lazy var composeButton: UIButton = {
        
        let button = UIButton()
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        
         button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        
        self.addSubview(button)
        
        return button
        
        
        }()
    
    
}
